from symbol import return_stmt

import FindXY
import FindFace
import GetScreen
import GetTxt
import random
import cv2
import pyautogui
import pyperclip

if __name__ == '__main__':

    k = 0

    while True:

        cv2.waitKey(3000)

        # 获取截图
        tempScreenName = "temp/screen" + str(random.randint(1, 1000000)) + ".png"
        GetScreen.capture_screen_and_save(tempScreenName)
        img = cv2.imread(tempScreenName)

        while True:

            # 等待1秒
            cv2.waitKey(200)

            # 查找是否有点赞
            top_left, bottom_right, center, max_val = FindXY.find_template_in_image("img/nice.png",
                                                                                    tempScreenName)

            print(f"点赞位置：{center}")


            if max_val > 0.95:
                print("已点赞")
                cv2.rectangle(img, top_left, bottom_right, (0, 0, 255), 20)

                # 保存更新
                cv2.imwrite(tempScreenName, img)

                if (center[1] - 100) < 230:
                    continue

                # 移动光标并点击
                pyautogui.moveTo(center[0], center[1] - 100, duration=0.2)  # duration 是移动所需时间(秒)
                pyautogui.click(center[0], center[1] - 100)

                cv2.waitKey(random.randint(5000, 10000))

                # 查找是否收藏
                tempScreenName1 = "temp/screen" + str(random.randint(1, 1000000)) + ".png"
                GetScreen.capture_screen_and_save(tempScreenName1)

                top_left1, bottom_right1, center1, max_val1 = FindXY.find_template_in_image("img/not like.png",
                                                                                        tempScreenName1)
                if max_val1 > 0.9:
                    print("未收藏")

                    #点击收藏
                    pyautogui.moveTo(center1[0], center1[1], duration=0.4)  # duration 是移动所需时间(秒)
                    pyautogui.click(center1[0], center1[1])

                    print("收藏完成")

                    # cv2.waitKey(random.randint(500, 1000))
                    #
                    # # 往左移动 开始评论
                    # pyautogui.moveTo(center1[0] - 200, center1[1], duration=0.4)  # duration 是移动所需时间(秒)
                    # pyautogui.click(center1[0] - 200, center1[1])
                    #
                    # cv2.waitKey(random.randint(500, 1000))
                    #
                    # # 输入评论
                    # comment = GetTxt.random_compliment("1")
                    # pyperclip.copy(comment)  # 使用 pyperclip 操作剪贴板
                    # pyautogui.hotkey('ctrl', 'v')  # Windows/Linux
                    # cv2.waitKey(random.randint(500, 1000))
                    # pyautogui.press('enter')  # 输入完后按下回车

                    print("评论完成")


                    k = k + 1

                    if k > 500:
                        break

                # 等待1~3秒
                cv2.waitKey(random.randint(1000, 3000))


                top_left1, bottom_right1, center1, max_val1 = FindXY.find_template_in_image("img/follow.png",
                                                                                            tempScreenName1)

                if max_val1 > 0.85:
                    print("开始关注")

                    # 点击关注坐标
                    pyautogui.moveTo(center1[0], center1[1], duration=0.4)  # duration 是移动所需时间(秒)
                    pyautogui.click(center1[0], center1[1])

                    # 等待1~3秒
                    cv2.waitKey(random.randint(500, 1000))

                    print("关注完成")

                else:

                    # img = cv2.imread(tempScreenName1)
                    # # 标记收藏位置
                    # cv2.rectangle(img, top_left1, bottom_right1, (0, 0, 255), 20)
                    # #展示
                    # cv2.imshow("result", img)
                    # cv2.waitKey(0)


                    print(f"已关注：{max_val1}")

                # 等待1~3秒
                cv2.waitKey(random.randint(1000, 3000))

                #  退出
                pyautogui.moveTo(150, 500, duration=0.4)  # duration 是移动所需时间(秒)
                pyautogui.click(150, 500)

                # 等待1~3秒
                cv2.waitKey(random.randint(1000, 10000))

                # 显示结果
                # cv2.imshow("result", img)
                # cv2.waitKey(0)

            else:

                break

        if k > 500:
            break

        pyautogui.moveTo(400, 400, duration=0.4)  # duration 是移动所需时间(秒)
        cv2.waitKey(random.randint(200, 500))
        pyautogui.scroll(-200)
        cv2.waitKey(random.randint(200, 500))
        pyautogui.scroll(-200)
        cv2.waitKey(random.randint(200, 500))
        pyautogui.scroll(-200)
        cv2.waitKey(random.randint(200, 500))
        pyautogui.scroll(-200)
